package ans.p36;

import test.Entrance;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

/**
 * #      36
 * 名称   有效的数独
 * 链接   https://leetcode-cn.com/problems/valid-sudoku
 * 题解   [805](https://leetcode-cn.com/problems/valid-sudoku/solution)
 * 通过率 62.2%
 * 难道   中等
 */

public class Solution {
    public boolean putValue(Set<Character>[] list, int index, Character value) {
        Set<Character> characters = list[index];
        if (characters == null) {
            characters = new HashSet<>();
            characters.add(value);
            list[index] = characters;
            return true;
        } else {
            return characters.add(value);
        }
    }

    @Entrance
    public boolean isValidSudoku(char[][] board) {
        Set<Character>[] listX = new Set[9];
        Set<Character>[] listY = new Set[9];
        Set<Character>[] listZ = new Set[9];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if ('.' != board[i][j]) {
                    Character c = board[i][j];
                    if (!putValue(listX, i, c)) {
                        return false;
                    }
                    if (!putValue(listY, j, c)) {
                        return false;
                    }
                    if (!putValue(listZ, i / 3 * 3 + j / 3, c)) {
                        return false;
                    }
                }
            }
        }


        return true;
    }


}